const port = config.port
const $url = "http://127.0.0.1:" + port

let resss
async function getPID(name){
    resss = await http(
        {
        url: $url+"/AppStatue",
        type: "POST",
        data: {switch:name},
        header:{contextType: "application/json"},
    })
    // console.log(13,resss)
    return resss
   
    
}
// var switchList = ['identify1','identify2','identify3']
var ButtonInfo
var switchList
async function getButtonInfo(){
    ButtonInfo = await http(
        {
        url: $url+"/getButtonInfo",
        type: "get",
        header:{contextType: "application/json"},
    })
    switchList = Object.keys(ButtonInfo);
    // <button  id="identify1">打开视觉识别1</button>
    var str2 = ''
    for (let i=0;i<switchList.length;i++){
        str2 = str2 +'<div style="margin-top:5px;"><button   id="'+ switchList[i] +'">'+ ButtonInfo[switchList[i]].name +'</button></div>'
    }
    $("#buttonList").html(str2);
    for (let i = 0; i < switchList.length; i++) {
        let element = document.getElementById(switchList[i]);
        if (element) {
            element.addEventListener('click', function() {
                controlexe(switchList[i]);
        });
    }
}
}
getButtonInfo()



async function monitor(){
    var pidList = []
    // console.log(36,switchList)
    for(let i = 0;i<switchList.length-1;i++){
        var pid =await getPID(switchList[i])
        pidList.push(...pid.msg)
    }
    // console.log(53,pidList)
    var str2 = '<tr><th>应用PID</th><th>应用名称</th><th>占用内存</th><th>应用描述</th><th>操作</th></tr>'
    for (let i=0;i<pidList.length-1;i++){
        str2= str2 + '<tr><td>'+pidList[i][1]+'</td><td>'+pidList[i][0]+'</td><td>'+
        pidList[i][4] + ' K </td><td>正在运行</td><td><button onclick="closeEXE('+"'"+pidList[i][0]+"',"+pidList[i][1]+')">关闭</button></td></tr>'
    }
    // console.log(str2)
    $("#tableID").html(str2);
}
// monitor()
setInterval(function() {monitor()},1000)

// getPID('openexe')
async function controlexe(name) {
    let result = await http(
        {
        url: $url+"/Controlexe",
        type: "POST",
        data:{switch:name},
        header:{contextType: "application/json"},
    })
    alert(result.msg)
    // console.log(47,result)
    
  }
  
 // 控制程序开关


// document.getElementById('identify1').addEventListener('click', function() {
//     controlexe('identify1')
// });
// document.getElementById('identify2').addEventListener('click', function() {
//     controlexe('identify2')
// });
// document.getElementById('identify3').addEventListener('click', function() {
//     controlexe('identify3')
// });

async function closeEXE(name,PID){
    // console.log(95,name,PID)
    let result = await http(
        {
        url: $url+"/closeEXE",
        type: "POST",
        data:{name:name,PID:PID},
        header:{contextType: "application/json"},
    })
    if (result.Code == 200){
        alert("关闭成功")
    }else{
        alert("关闭失败")
    }
    
    // console.log(result)
}